
var fs = require('fs')
var path = require('path')
var uglifyjs = require("uglify-js")


//将非stringl类型递归转化为string
xToString = function(obj)
{
    if(typeof(obj) == "string")
        return obj
    else if (typeof(obj) == "boolean")
        if(obj === true)
            return 'true'
        else
            return 'false'
    else if (typeof(obj) == "number")
        return obj
    else if (typeof(obj) == "object")
    {
        if (obj instanceof Array == true)
        {
            var str = ''
            for (var i=0;i<obj.length;i++)
            {    
                if (i==0) 
                {
                    str = str + xToString(obj[i])
                }
                else
                {
                    str = str + ',' + xToString(obj[i])
                }
            }   
            //str = str + ']'
            return str
        }
        else
        {
            return JSON.stringify(obj)  //json对象转换成json对符串 
        }
    }
}


currentTime = function()
{ 
        var now   = new Date();
        var year  = now.getFullYear();       //年
        var month = now.getMonth() + 1;     //月
        var day   = now.getDate();            //日
        var hh    = now.getHours();            //时
        var mm    = now.getMinutes();          //分
        var clock = year + "-";
        if(month < 10)
            clock += "0";
        clock += month + "-";
        if(day < 10)
            clock += "0";
        clock += day + " ";
        if(hh < 10)
            clock += "0";
        clock += hh + ":";
        if (mm < 10) clock += '0'; 
        clock += mm; 
        return(clock); 
} 


clone = function(obj){  
    var o;  
    switch(typeof obj){  
    case 'undefined': break;  
    case 'string'   : o = obj + '';break;  
    case 'number'   : o = obj - 0;break;  
    case 'boolean'  : o = obj;break;  
    case 'object'   :  
        if(obj === null){  
            o = null;  
        }else{  
            if(obj instanceof Array){  
                o = [];  
                for(var i = 0, len = obj.length; i < len; i++){  
                    o.push(clone(obj[i]));  
                }  
            }else{  
                o = {};  
                for(var k in obj){  
                    o[k] = clone(obj[k]);  
                }  
            }  
        }  
        break;  
    default:          
        o = obj;break;  
    }  
    return o;     
}  


mkdirS = process.platform=='win32'?'mkdir ':'mkdir -p '
rmdirS = process.platform=='win32'?'rmdir /s /q ':'rm -rf '

chmod777S = function(path)
{
    return process.platform=='win32'?'cacls ' +  path  + ' /e /t /c /g everyone:F':'chmod -R 777 ' + path
}

// 逻辑符号  正则没有对本身的逻辑运算符
// !!表示取反 
// &&表示并且 
// ||表示或者
// ((  xxxxx  )) 内容块  
// !! && ||属于同级
isMatch_regexp = function(regexpStr, targetStr)
{   
    //匹配最外层的括号内内容
    //parenthesesStr  第一个匹配到的括号内内容 带括号的 
    //如果没有匹配到则将参数字符串返回
    //特别的如果parenthesesRightStr前面有！！需要并入
    function getFirstParenthesesStr(regexpStr)
    {
        var parenthesesStr
        var parenthesesLeftStr
        var parenthesesRightStr

        var searchStartIdx = regexpStr.search(/\(\(/)
        if( searchStartIdx >=0 )
        {   
            var regexpStrOther = regexpStr.substr(searchStartIdx+2)
            var searchEndIdx = searchStartIdx  //searchStartIdx searchEndIdx 间是一段包括括号的 字符串
 
            for(var count=1;count>0;)
            {
                var searchStart1 = regexpStrOther.search(/\(\(/)
                var searchStart2 = regexpStrOther.search(/\)\)/)
                if(searchStart1<searchStart2)
                {
                    count=count+1
                    regexpStrOther = regexpStrOther.substr(searchStart1+2)
                    searchEndIdx = searchEndIdx + searchStart1+2
                }
                else
                {
                    count=count-1
                    regexpStrOther = regexpStrOther.substr(searchStart2+2)
                    searchEndIdx = searchEndIdx + searchStart2+2
                }
            }

            parenthesesRightStr = regexpStr.substr(searchEndIdx+2)
            if(searchStartIdx>=2&&regexpStr.substr(searchStartIdx-2,2) == '!!')
            {
                parenthesesLeftStr = regexpStr.substr(0, searchStartIdx-2)
                parenthesesStr = regexpStr.substr(searchStartIdx-2, searchEndIdx-searchStartIdx+2)
            }
            else
            {
              parenthesesLeftStr = regexpStr.substr(0, searchStartIdx)
              parenthesesStr = regexpStr.substr(searchStartIdx, searchEndIdx-searchStartIdx+2)
            }
        }
        else
        {
            return regexpStr
        }
        //console.log('getFirstParenthesesStr:',parenthesesStr, parenthesesLeftStr, parenthesesRightStr)
        return [parenthesesStr, parenthesesLeftStr, parenthesesRightStr]
    }

    //匹配最后一个前面带 || &&  操作符的regexpStr 连带操作符一起返回 操作符跟在operatorRightStr上
    //如果没有匹配到则将参数字符串返回
    function getLastOperatorRightStr(regexpStr)
    {
        var operatorLefttStr
        var operatorRightStr

        var lastIndex1 = regexpStr.lastIndexOf('&&')
        var lastIndex2 = regexpStr.lastIndexOf('||')

        var lastIndex = lastIndex1>lastIndex2?lastIndex1:lastIndex2

        if(lastIndex>=0)
        {
            operatorLefttStr = regexpStr.substr(0, lastIndex)
            operatorRightStr = regexpStr.substr(lastIndex)
        }
        else
        {
            return regexpStr
        }

        return [operatorLefttStr, operatorRightStr]
    }


    var str = getFirstParenthesesStr(regexpStr)

    //console.log(str, regexpStr)
    if( str != regexpStr) //匹配到括号
    {
        var parenthesesStr = str[0]
        var parenthesesLeftStr = str[1]
        var parenthesesRightStr = str[2]

        var isMatch = true
        //console.log('1111:',parenthesesStr)
        if(parenthesesStr.substr(0,2) == '!!')
        {
           var parenthesesStr = parenthesesStr.substr(4,parenthesesStr.length-6)
           //console.log('22222:',parenthesesStr)

           isMatch = isMatch && !isMatch_regexp(parenthesesStr, targetStr)
        }
        else
        {   
           var parenthesesStr = parenthesesStr.substr(2,parenthesesStr.length-4)
           //console.log('33333:',parenthesesStr)
           isMatch = isMatch && isMatch_regexp(parenthesesStr, targetStr)
        }

        if(parenthesesLeftStr)
        {
            var operator = parenthesesLeftStr.substr(-2)
            if(operator=='||')
            {
                isMatch = isMatch ||  isMatch_regexp( parenthesesLeftStr.substr(0, parenthesesLeftStr.length-2), targetStr )
            }
            else if(operator=='&&')
            {
                isMatch = isMatch &&  isMatch_regexp( parenthesesLeftStr.substr(0, parenthesesLeftStr.length-2), targetStr )
            }
        }

        if(parenthesesRightStr)
        {
            var operator = parenthesesRightStr.substr(0,2)
            //console.log('4444',parenthesesRightStr)
            if(operator=='||')
            {
                var parenthesesRightStr = parenthesesRightStr.substr(2)
                //console.log('5555',parenthesesRightStr)

                isMatch = isMatch ||  isMatch_regexp( parenthesesRightStr, targetStr )
            }
            else if(operator=='&&')
            { 
                var parenthesesRightStr = parenthesesRightStr.substr(2)
                isMatch = isMatch &&  isMatch_regexp( parenthesesRightStr, targetStr )
            }
        }

        return isMatch
    }
    else //没有括号
    {   
        var str = getLastOperatorRightStr(regexpStr)
        if(str != regexpStr)
        {   
            var operatorLefttStr = str[0]
            var operator = str[1].substr(0,2)
            var operatorRightStr = str[1].substr(2)

            if(operator == '||')
            {
                return isMatch_regexp(operatorLefttStr, targetStr) || isMatch_regexp(operatorRightStr, targetStr)

            }
            else if(operator == '&&')
            {
                return isMatch_regexp(operatorLefttStr, targetStr) && isMatch_regexp(operatorRightStr, targetStr)
            }
        }
        else //没有匹配碰到的第一个前面带 || &&  操作符的regexpStr
        {
            if(str.substr(0,2) == '!!')
                return !targetStr.match( new RegExp(str.substr(2)) ) 
            else
                return targetStr.match( new RegExp(str) )
        }
    }

}


walkFiles = function(filePath, floor, handleFile) 
{  
    handleFile(filePath, floor)  
    floor++  
    var files = fs.readdirSync(filePath)
    files.forEach(function(item) 
    {  
        var tmpPath = filePath + '/' + item  
        if(fs.statSync(tmpPath).isDirectory()) 
        {
            walkFiles(tmpPath, floor, handleFile)
        }
        else
        {
            handleFile(tmpPath, floor)  
        }
    }) 
}  


minifyJs = function(srcPaths, targetPath) 
{
    var jsArray = []

    for(var i in srcPaths)
    {   
        var srcPath = srcPaths[i]
        if(path.extname(srcPath) == '.js')
        {
            jsArray[jsArray.length] = srcPath
        }
        else
        {
            walkFiles(srcPath, 0, handleFile) 
        }
    }

    function handleFile(filePath) 
    {       
        if(path.extname(filePath) == '.js')
            {
            jsArray[jsArray.length] = filePath
        }
    }  
    var result = uglifyjs.minify(jsArray)
    fs.writeFile(targetPath, result.code)
}



